package collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class GenericDemo {
    public static void main(String[] args) {
        Collection<String> c = new ArrayList<>(); //泛型的集合
        c.add("one");
        c.add("two");
        c.add("three");
        c.add("four");
        c.add("five");
        //c.add(123);//编译错误，123的类型违背了集合c所指向的实际类型String

        Iterator<String> it = c.iterator();
        while(it.hasNext()){
            String str = it.next();
            System.out.println(str);
        }
        for(String str : c){
            System.out.println(str);
        }
        Collection<Point> c1 = new ArrayList<>();
        c1.add(new Point(1,2));
        c1.add(new Point(3,4));
        c1.add(new Point(5,6));
        c1.add(new Point(7,8));

        Iterator<Point> in = c1.iterator();
        while (in.hasNext()){
            Point p = in.next();
            System.out.println(p);
        }
        for(Point p : c1){
            System.out.println(p);
        }
    }
}
